package edu.ccut.saturn.studio.wfg.wfgmodel;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

import edu.ccut.saturn.studio.Activator;
import edu.ccut.saturn.studio.Images;
import edu.ccut.saturn.studio.core.search.IObserver;
import edu.ccut.saturn.studio.core.search.SaturnFileSearcher;
import edu.ccut.saturn.studio.core.search.SaturnFileSearcher.SearchResult;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.core.util.freemaker.FreemarkerUtils;
import edu.ccut.saturn.studio.core.util.xml.SaturnW3CUtil;
import edu.ccut.saturn.studio.export.createhtml.Parameters;
import edu.ccut.saturn.studio.wfg.dialog.humanactivitydialog.SaturnHumanActivityDialog;
import edu.ccut.saturn.studio.wfg.figure.SaturnFigure;
import edu.ccut.saturn.studio.wfg.model.Shape;

public class SaturnHumanNode extends Shape {

	private static final long serialVersionUID = 1L;
	
	SaturnMultiWork saturnMultiWork = new SaturnMultiWork();
	
	SaturnParticipator saturnParticipator = new SaturnParticipator();
	
	private String url = "";

	private String timeLimit = "";

	private String polymeMode = "1";
	
	private String afterRequest = "";

	private List<SaturnAttribute> logicKeyList = new ArrayList<SaturnAttribute>();

	private List<SaturnAttribute> formDataAttributeList = new ArrayList<SaturnAttribute>();
	
	private Image humanPictureSmall = createImage(Activator.SMALL_HUMAN_ICON);
	
	private static int number = 1;
	
	public SaturnHumanNode() {
		super.setName(SaturnName.KEY_NODE_NAME_RENGONGHUODONG + number++);
		super.setSize(new Dimension(50, 50));
	}

	public String getType(){
		return SaturnName.KEY_NODE_ATTRIBUTE_TYPENAMEHUMAN;
	}
	
	@Override
	public IFigure getFigure() {
		SaturnFigure saturnFigure = new SaturnFigure();
		saturnFigure.setIcon(Activator.getImage(Images.BIG_HUMAN_ICON));
		saturnFigure.setName(super.getName());
		return saturnFigure;
	}
	
	public Dialog getDialog() {
		SaturnHumanActivityDialog dialog = new SaturnHumanActivityDialog(Display.getDefault().getShells()[0], this);
		return dialog;
	}

	@Override
	protected Node createChildNode(Document document, SaturnAttribute attrib, String name) {
		return attrib.creatNode(document, name);
	}

	@Override
	public Node doRefresh(Document document) {
		Node startHumanNode = document.createElement(SaturnName.XML_NAME);
		Node rootNode = document.getDocumentElement();
		// 基本信息
		SaturnW3CUtil.createAttribute(startHumanNode, SaturnName.XML_TYPE, getType());
		SaturnW3CUtil.createChildNode(startHumanNode, SaturnName.KEY_NODE_ELEMENT_TIMELIMIT, this.timeLimit);
		SaturnW3CUtil.createChildNode(startHumanNode, SaturnName.KEY_NODE_ELEMENT_POLYMEMODE, this.polymeMode);
		SaturnW3CUtil.createChildNode(startHumanNode, SaturnName.KEY_NODE_ELEMENT_AFTERREQUEST, this.afterRequest);
		
		// logicKey
		super.createNodes(document, startHumanNode, this.getLogicKeyList(), SaturnName.KEY_NODE_ELEMENT_LOGICKEY, SaturnName.KEY_NODE_NAME_KEY);
		// formData
		SaturnW3CUtil.createChildNode(startHumanNode, SaturnName.KEY_NODE_ELEMENT_URL, this.url);
		super.createNodes(document, startHumanNode, this.getFormDataAttributeList(),SaturnName.KEY_NODE_ELEMENT_FORMDATA,SaturnName.KEY_NODE_NAME_DATA);
		
		saturnMultiWork.createNode(document, startHumanNode);
		saturnParticipator.createNode(document, startHumanNode);

		rootNode.appendChild(startHumanNode);
		return startHumanNode;
	}
	
	
	@Override
	public Shape cloneShape(Shape shape) {
		SaturnHumanNode saturnHumanNode = (SaturnHumanNode)shape;
		SaturnHumanNode newSaturnHumanNode = new SaturnHumanNode();
		newSaturnHumanNode.setName(saturnHumanNode.getName());
		newSaturnHumanNode.setId(saturnHumanNode.getId());
		newSaturnHumanNode.setNextId(saturnHumanNode.getNextId());
		newSaturnHumanNode.setDescription(saturnHumanNode.getDescription());
		newSaturnHumanNode.setUrl(saturnHumanNode.getUrl());
		newSaturnHumanNode.setTimeLimit(saturnHumanNode.getTimeLimit());
		newSaturnHumanNode.setPolymeMode(saturnHumanNode.getPolymeMode());
		newSaturnHumanNode.setSmallIcon(saturnHumanNode.getSmallIcon());
		return newSaturnHumanNode;
	}

	public SaturnMultiWork getSaturnMultiWork() {
		return saturnMultiWork;
	}

	public void setSaturnMultiWork(SaturnMultiWork saturnMultiWork) {
		this.saturnMultiWork = saturnMultiWork;
	}

	public SaturnParticipator getSaturnParticipator() {
		return saturnParticipator;
	}

	public void setSaturnParticipator(SaturnParticipator saturnParticipator) {
		this.saturnParticipator = saturnParticipator;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getTimeLimit() {
		return timeLimit;
	}

	public void setTimeLimit(String timeLimit) {
		this.timeLimit = timeLimit;
	}

	public String getPolymeMode() {
		return polymeMode;
	}

	public void setPolymeMode(String polymeMode) {
		this.polymeMode = polymeMode;
	}

	public String getAfterRequest() {
		return afterRequest;
	}

	public void setAfterRequest(String afterRequest) {
		this.afterRequest = afterRequest;
	}

	public List<SaturnAttribute> getLogicKeyList() {
		return logicKeyList;
	}

	public void setLogicKeyList(List<SaturnAttribute> logicKeyList) {
		this.logicKeyList = logicKeyList;
	}

	public List<SaturnAttribute> getFormDataAttributeList() {
		return formDataAttributeList;
	}

	public void setFormDataAttributeList(List<SaturnAttribute> formDataAttributeList) {
		this.formDataAttributeList = formDataAttributeList;
	}

	public Image getIcon() {
		return Activator.getImage(Images.BIG_HUMAN_ICON);
	}
	
	public Image getSmallIcon() {
		return humanPictureSmall;
	}

	public void setSmallIcon(Image humanPictureSmall) {
		this.humanPictureSmall = humanPictureSmall;
	}
	
	public List<SearchResult> keyMapInWorkspace(IObserver observer) {
		
		List<SearchResult> list = null;
		String[] suffixes = {"logic"};
		
		try {
			list = SaturnFileSearcher.getValueAndDocumentMapInWorkspace(suffixes , observer, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	
	
	public List<SearchResult> keyMapInProject(IObserver observer) {
		List<SearchResult> list = null;
		
		try {
			list = SaturnFileSearcher.getValueAndDocumentMapInProject(super.findTheProject(), "logic",
					observer, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public Parameters createNodeHtml(String filePath) {
		
		Map<String, String> infos = new HashMap<String, String>();
		String name = getName();
		Parameters para = new Parameters();

		infos.put("name", getName());
		infos.put("id", getId());
		infos.put("polymtype", transfer(getPolymeMode()));
		infos.put("timelimit", getTimeLimit());
		infos.put("description", getDescription());

		para.setInfourl(name + ".html");
		para.setInfoname(name);
		para.setDescription(getDescription());
		para.setNodeType(Messages.getString("Wfg.HumanActivityNode")); //$NON-NLS-1$
		para.setImagename("human");
		String templatePath = "template/export/wfg/human";
		String Path = filePath + File.separator + name + ".html";
		FreemarkerUtils.writeTemplateToFile(templatePath, infos, Path);
		
		return para;
	}
	
	/**
	 * 对聚合模式进行转换
	 * 
	 * @param startPolicy
	 * @return Parameters
	 */
	private static String transfer(String polymeMode) {
		if (polymeMode.equals("1")) {
			return Messages.getString("Wfg.OnePoly"); //$NON-NLS-1$
		} else if (polymeMode.endsWith("2")) {
			return Messages.getString("Wfg.AllPoly"); //$NON-NLS-1$
		} else {
			return Messages.getString("Wfg.ManyPoly"); //$NON-NLS-1$
		}
	}
}
